package com.example.springboot.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.UserSerive.NoticeService;
import com.example.springboot.UserSerive.RoleService;
import com.example.springboot.pojo.Notice;
import com.example.springboot.pojo.Role;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    //new SpUserMapper
//    @Autowired
//    private SpUserMapper spUserMapper;
    @Autowired
    private NoticeService noticeService;
    //查
    @GetMapping
    public List<Notice> index(){
        return noticeService.list();
    }
    //新增与更新
    @PostMapping
    public boolean saveUser(@RequestBody Map map){
        Object o = map.get("notice");
        Notice notice = JSON.parseObject(JSON.toJSONString(o), Notice.class);
        Object name = map.get("name");
        //return spUserMapper.in(spUser);
        if(notice.getId()==null){
            notice.setTime(DateUtil.now());
            notice.setUser((String) name);
        }
        return noticeService.saveOrUpdate(notice);//plus
    }
    //删
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        //return spUserMapper.deleteId(id);
        return noticeService.removeById(id);
    }
    //多删除  /{id}只能有一个参数
    @DeleteMapping("/all")
    public boolean deleteAll(@RequestBody List<Integer> ids){
        //return spUserMapper.deleteId(id);
        return noticeService.removeByIds(ids);
    }

    //分页


//    @GetMapping("/page")
//    public  Map<String,Object> Page(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String username){
//        pageNum=(pageNum-1)*pageSize;
//        username="%"+username+"%";
//        List<SpUser> data=spUserMapper.selectPage(pageNum,pageSize,username);
//        Integer total=spUserMapper.selectToal();
//        Map<String,Object> res=new HashMap<>();
//        res.put("data",data);
//        res.put("total",total);
//       return res;
//    }

//分页plus

    @GetMapping("/page")
    public  IPage<Notice> Page(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam(defaultValue = "") String name){

        IPage<Notice> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<>();
        if(!"".equals(name)){queryWrapper.like("name",name);}
        queryWrapper.orderByDesc("id");
        return noticeService.page(page,queryWrapper);
    }










}
